<?php

namespace Admin\Controller;

use Think\Controller;

class GoodsController extends Controller
{
    //显示和处理表单
    public function add(){
        //判断用户是否提交了表单
        if(IS_POST){  //var_dump($_POST);die;
            $model = D('goods');
            if($model->create(I('post.'))){
                //插入到数据库中
                if($model->add()){
                    //显示成功信息并等待1秒之后跳转
                    $this->success();
                    exit;
                }
            }
            //从模型中取出失败的原因
            $error = $model->getError();
            //由控制器显示错误信息，并在3秒跳回上一个页面
            $this->error($error);

            $pics = array();
            foreach($_FILES['pic']['name'] as $k=>$v){
                $pics[] = array(
                    'name' => $v,
                    'type' => $_FILES['pic']['type'][$k],
                    'tmp_name' => $_FILES['pic']['tmp_name'][$k],
                    'error' => $_FILES['pic']['error'][$k],
                    'size' => $_FILES['pic']['size'][$k],
                );
            }
//            var_dump($pics);
//            var_dump($_FILES);die;
        }
        //取出所有的会员级别
        $mlModel = D('member_level');
        $mlData = $mlModel->select();
        //设置页面信息
        $this->assign(array(
            'mlData' => $mlData,
            '_page_title' => '添加新商品',
            '_page_btn_name' => '商品列表',
            '_page_btn_link' => U('lists'),
        ));
        //显示表单
        $this->display();
    }
    //商品列表页
    public function lists(){

        $model = D('goods');
        $data = $model->search();  //var_dump($data['data']);
//        $this->assign('list', $data['data']);
        $this->assign(array(
            '_page_title' => '商品列表',
            '_page_btn_name' => '添加新商品',
            '_page_btn_link' => U('add'),
            'list' => $data['data'],
            'page' => $data['page'],
        ));

        $this->display();
    }

    public function edit(){
        $id = I('get.id'); //要修改的商品的id
        $model = D('goods');
        //判断用户是否提交了表单
        if(IS_POST){
            $model = D('goods');
            if($model->create(I('post.'),2)){
                //插入到数据库中
                if(FALSE !== $model->save()){
                    //显示成功信息并等待1秒之后跳转
                    $this->success();
                    exit;
                }
            }
            //从模型中取出失败的原因
            $error = $model->getError();
            //由控制器显示错误信息，并在3秒跳回上一个页面
            $this->error($error);
        }

        $data = $model->find($id);
        $this->assign('data',$data);

        //取出所有的会员级别
        $mlModel = D('member_level');
        $mlData = $mlModel->select();

        $mpModel = D('member_price');
        $mpData = $mpModel->where(array(
            'goods_id' => array('eq',$id)
        ))->select();
        //把二维转一维
        $_mpData = array();
        foreach ($mpData as $k => $v)
        {
            $_mpData[$v['level_id']] = $v['price'];
        }

        $gpModel = D('goods_pic');
        $gpData = $gpModel->field('id,mid_pic')->where(array(
            'goods_id' => array('eq',$id),
        ))->select();

        $gcModel = D('goods_cat');
        $gcData = $gcModel->field('cat_id')->where(array(
            'goods_id' => array('eq',$id),
        ))->select();

        $catModel = D('category');
        $catData = $catModel->getTree();

        //设置页面信息
        $this->assign(array(
            'catData' => $catData,
            'mlData' => $mlData,
            'mpData' => $_mpData,
            'gpData' => $gpData,
            'gcData' => $gcData,
            '_page_title' => '修改商品',
            '_page_btn_name' => '商品列表',
            '_page_btn_link' => U('lists'),
        ));
        //显示表单
        $this->display();
    }
    //处理AJAX删除图片的请求
    public function ajaxDelPic()
    {
        $picId = I('get.picid');
        $gpModel = D('goods_pic');
        $pic = $gpModel->field('pic,sm_pic,mid_pic,big_pic')->find($picId);
        deleteImage($pic);
        $gpModel->delete($picId);
    }

}